#include <sys/wait.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>

#include <signal.h>

void pr_exit(int status){
    if(WIFEXITED(status)){
        printf("normal termination, exit status = %d\n", WEXITSTATUS(status));
    } else if (WIFSIGNALED(status)){
        printf("abnormal termination, exit status = %d%s\n", WTERMSIG(status), 
#ifdef WCOREDUMP
            WCOREDUMP(status) ? "(corefile generated)" : "");
#else
        "");
#endif
    } else if (WIFSTOPPED(status)){
        printf("child stopped, signal number = %d\n", WSTOPSIG(status));
    }
}

int main(){
    pid_t pid;
    int status;

    if((pid = fork()) < 0){
        printf("fork error\n");
        exit(0);
    }else if(pid == 0){
        exit(7);
    }

    if(pid != wait(&status)){
        printf("wait error\n");
        exit(0);
    }

    pr_exit(status);

    if((pid = fork()) < 0){
        printf("fork error\n");
        exit(0);
    }else if(pid == 0){
        abort();
    }

    if(pid != wait(&status)){
        printf("wait error\n");
        exit(0);
    }

    pr_exit(status);
    
    if((pid = fork()) < 0){
        printf("fork error\n");
        exit(0);
    }else if(pid == 0){
        status /= 0;
    }

    if(pid != wait(&status)){
        printf("wait error\n");
        exit(0);
    }

    pr_exit(status);

    return 0;
}